package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.params.OrderListParams;
import com.ruoyi.system.domain.params.SaveOrderParams;
import com.ruoyi.system.domain.vo.OrderListVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.WhOrder;
import com.ruoyi.system.service.IWhOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客户订单Controller
 *
 * @author ruoyi
 * @date 2023-02-26
 */
@Slf4j
@RestController
@RequestMapping("/system/order")
public class WhOrderController extends BaseController {
    @Resource
    private IWhOrderService whOrderService;

    /**
     * 查询客户订单列表
     */
    @PreAuthorize("@ss.hasPermi('system:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(OrderListParams params) {
        startPage();
        List<OrderListVo> list = whOrderService.selectWhOrderList(params);
        return getDataTable(list);
    }

    /**
     * 导出客户订单列表
     */
    @PreAuthorize("@ss.hasPermi('system:order:export')")
    @Log(title = "客户订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderListParams params) {
        List<OrderListVo> list = whOrderService.selectWhOrderList(params);
        ExcelUtil<OrderListVo> util = new ExcelUtil<OrderListVo>(OrderListVo.class);
        util.exportExcel(response, list, "客户订单数据");
    }

    /**
     * 获取客户订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return success(whOrderService.selectWhOrderById(id));
    }

    /**
     * 获取编辑详情
     */
    @PreAuthorize("@ss.hasPermi('system:order:query')")
    @GetMapping(value = "/edit/info/{id}")
    public AjaxResult getEditInfo(@PathVariable("id") Integer id) {
        return success(whOrderService.selectEditOrderById(id));
    }

    /**
     * 新增客户订单
     */
    @PreAuthorize("@ss.hasPermi('system:order:add')")
    @Log(title = "客户订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SaveOrderParams params) {
        params.setCreateBy("s_" + getUserId());
        params.setCreateTime(new Date());
        return success(whOrderService.insertWhOrder(params));
    }

    /**
     * 修改客户订单
     */
    @PreAuthorize("@ss.hasPermi('system:order:edit')")
    @Log(title = "客户订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SaveOrderParams params) {
        params.setUpdateBy("s_" + getUserId());
        params.setUpdateTime(new Date());
        return toAjax(whOrderService.updateWhOrder(params));
    }

    /**
     * 删除客户订单
     */
    @PreAuthorize("@ss.hasPermi('system:order:remove')")
    @Log(title = "客户订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(whOrderService.deleteWhOrderByIds(ids));
    }

    /**
     * 打印订单 - 支持批量
     * @param ids
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:order:edit')")
    @PostMapping("/print")
    public AjaxResult printOrder(@PathVariable Integer[] ids) {
        try {
            whOrderService.printOrder(ids,(getUserId() + ""));
            return success("打印队列执行成功");
        }catch (Exception e) {
            log.error("打印订单发生错误：{}", e.getMessage());
            return AjaxResult.error("打印订单发生错误", e.getMessage());
        }
    }
}
